-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====

--[ redirige le bon dialogue ne fonction du lieu ]--
function storyboard_ProcessDialog()
   local texte = SbQuestEdit:GetText();
   if texte ~= "" then
   
      STORYBOARD_NPCSAY = 1
      if ( STORYBOARD_SAVED.INFO_SAY_MODE ) then
     SendChatMessage(texte,"SAY");
      end
   
      storyboard_sendTextGreen("\n"..UnitName("player").." :");
      storyboard_sendText(texte)
      
      if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_TALK_IN_LOCATION then
      storyboard_useObjectAndKeywordsOnLocation(texte)
      end
      
      if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_SPEAK_WITH_TARGET and UnitExists("target") then      
      storyboard_useObjectAndKeywordsOnTarget(texte)
      end
      
      SbQuestEdit:SetText("");
   end
end


-- [ Managment of event ]--
function storyboard_manageGeneralEvent(event)

   STORYBOARD_FS_EVENT = event
   
   if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_EXPLORE_LOCATION then
   SBMessageManagerFrame:SetText(" ");
   local haveAnAction = storyboard_useObjectOnLocation()
   if haveAnAction then
      SbQuestFrameNpcNameText:SetText(STORYBOARD_MES_INTERFACE_002)
      storyboard_showQuestFrame();
   end
   end
   
   if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_TALK_IN_LOCATION then
   SBMessageManagerFrame:SetText(" ");
   SbQuestFrameNpcNameText:SetText(STORYBOARD_MES_INTERFACE_003);
   storyboard_showQuestFrame()
   end
   
   if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_EXPLORE_TARGET then
   SBMessageManagerFrame:SetText(" ");
   if UnitExists("target") then
      
      if CheckInteractDistance("target", 1) == nil then
        storyboard_sendAlertMessage(STORYBOARD_MES_CONSOLEINFO_008,1);
    return 0
      end
      
      local haveAnAction = storyboard_useObjectOnTarget()
      if haveAnAction then
        local npc = UnitName("target")
        SbQuestFrameNpcNameText:SetText(STORYBOARD_MES_INTERFACE_004 .." "..npc)
    storyboard_showQuestFrame()
      end
   else
   storyboard_sendAlertMessage(STORYBOARD_MES_CONSOLEINFO_009,1);
   end
   end
   
   if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_SPEAK_WITH_TARGET then
   SBMessageManagerFrame:SetText(" ");
   if UnitExists("target") then
   
      if CheckInteractDistance("target", 1) == nil then
        storyboard_sendAlertMessage(STORYBOARD_MES_CONSOLEINFO_008,1);
    return 0
      end
      
      local npc = UnitName("target")
      SbQuestFrameNpcNameText:SetText(STORYBOARD_MES_INTERFACE_005 .." "..npc)
      storyboard_showQuestFrame()
   else
   storyboard_sendAlertMessage(STORYBOARD_MES_CONSOLEINFO_009,1);
   end
   end
   
   if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_BOUSSOLE then
   storyboard_boussole_printPosition();
   end
   
   if STORYBOARD_FS_EVENT == STORYBOARD_FS_EVENT_SCENAR_OPENING then
   storyboard_updateScenarioIntro()
   SbIntro:Show()
   end
   
end


-------------------------------------------------------------------------------
-- [ INTERACTION AVEC LES LIEUX ] --
-------------------------------------------------------------------------------

-- [ Declenchement d'un �venement fouiller ( pas de keywords )] --
function storyboard_useObjectOnLocation()
   storyboard_debugMessage("storyboard_useObjectOnLocation");
   
   local wellLocated = false;
   local areGoodConditions = false;

   for idLoc = 1, STORYBOARD_FS_NB_LOCATION, 1 do
      
      local idContinent   = STORYBOARD_SCENARIO_LOCATION[idLoc].idContinent
      local coordX        = STORYBOARD_SCENARIO_LOCATION[idLoc].coordX
      local coordY        = STORYBOARD_SCENARIO_LOCATION[idLoc].coordY
      local radiusZone    = STORYBOARD_SCENARIO_LOCATION[idLoc].radiusZone
      local playerLife    = STORYBOARD_SCENARIO_LOCATION[idLoc].playerLife
      local neededObjects = STORYBOARD_SCENARIO_LOCATION[idLoc].neededObjects
      local newObjects    = STORYBOARD_SCENARIO_LOCATION[idLoc].newObjects
      local lostObjects   = STORYBOARD_SCENARIO_LOCATION[idLoc].lostObjects
      local keywords      = STORYBOARD_SCENARIO_LOCATION[idLoc].keywords
      local comment       = STORYBOARD_SCENARIO_LOCATION[idLoc].comment
      
      -- si j'ai pas besoin d'incanter
      if not ( storyboard_AreYouNeedKeywords(keywords) ) then
     
     -- Si je suis au bon endroit
     if storyboard_location_playerIsOnLocation(idContinent,coordX,coordY,radiusZone) or idContinent==0 then

            wellLocated = true;     

        -- Si je suis dans le bon �tat corporel ( mort ou vivant )
        if storyboard_isTheSameLifeStateOnPlayer(playerLife) then

           -- si j'ai besoin d'objets
           if storyboard_AreYouNeedObjects(neededObjects) then
        
          -- Est ce que je les ai tous ?
          if storyboard_haveYouGotObjects(neededObjects) then
             storyboard_sendTextGreen("\n"..STORYBOARD_MES_INTERFACE_012);
             storyboard_sendTextYellow(comment)
             local mes = STORYBOARD_MES_INTERFACE_012 .. comment;
                 storyboard_writeMessageAnswerForOtherPlayer(mes);
             storyboard_setLostObjects(lostObjects)
             storyboard_setNewObjects(newObjects)
                     areGoodConditions = true;
          end
          
           -- si je n'ai pas besoin d'objets
           else
          storyboard_sendTextGreen("\n"..STORYBOARD_MES_INTERFACE_012);
          storyboard_sendTextYellow(comment)
          local mes = STORYBOARD_MES_INTERFACE_012 .. comment;
              storyboard_writeMessageAnswerForOtherPlayer(mes);
          storyboard_setLostObjects(lostObjects)
          storyboard_setNewObjects(newObjects)
                  areGoodConditions = true;
           end
        end
     end
      end
   end

   -- Si la recherche n'est pas r�f�renc� dans le sc�nario
   if (not wellLocated) or (not areGoodConditions) then
      -- on affiche un message par d�faut
      storyboard_sendAlertMessage(storyboard_defaultMessage(message,STORYBOARD_SCENARIO_DEFAULT_SEARCH),1);
      
   end

 return areGoodConditions;

end




-- [ Declenchement d'un �venement incantation ( dans un lieu )] --
function storyboard_useObjectAndKeywordsOnLocation(message)
   storyboard_debugMessage("storyboard_useObjectAndKeywordsOnLocation");
   
   local wellLocated = false;
   local areGoodConditions = false;
   
   for idLoc = 1, STORYBOARD_FS_NB_LOCATION, 1 do
      
      local idContinent   = STORYBOARD_SCENARIO_LOCATION[idLoc].idContinent
      local coordX        = STORYBOARD_SCENARIO_LOCATION[idLoc].coordX
      local coordY        = STORYBOARD_SCENARIO_LOCATION[idLoc].coordY
      local radiusZone    = STORYBOARD_SCENARIO_LOCATION[idLoc].radiusZone
      local playerLife    = STORYBOARD_SCENARIO_LOCATION[idLoc].playerLife
      local neededObjects = STORYBOARD_SCENARIO_LOCATION[idLoc].neededObjects
      local newObjects    = STORYBOARD_SCENARIO_LOCATION[idLoc].newObjects
      local lostObjects   = STORYBOARD_SCENARIO_LOCATION[idLoc].lostObjects
      local keywords      = STORYBOARD_SCENARIO_LOCATION[idLoc].keywords
      local comment       = STORYBOARD_SCENARIO_LOCATION[idLoc].comment
      
      -- si j'ai besoin d'incanter
      if storyboard_AreYouNeedKeywords(keywords) then
      
     -- Si je suis au bon endroit
     if storyboard_location_playerIsOnLocation(idContinent,coordX,coordY,radiusZone) or idContinent==0 then

        wellLocated = true;     

        -- si j'ai les bonnes paroles
        if storyboard_areMatchBetweenYourAnswerAndYourWords(message,keywords) then
                      
           -- Si je suis dans le bon �tat corporel ( mort ou vivant )
           if storyboard_isTheSameLifeStateOnPlayer(playerLife) then
           
          -- si j'ai besoin d'objets
          if storyboard_AreYouNeedObjects(neededObjects) then
           
             -- Est ce que je les ai tous ?
             if storyboard_haveYouGotObjects(neededObjects) then
                storyboard_sendTextGreen("\n"..STORYBOARD_MES_INTERFACE_012);
            storyboard_sendTextYellow(comment)
            local mes = STORYBOARD_MES_INTERFACE_012 .. comment;
                    storyboard_writeMessageAnswerForOtherPlayer(mes);
             
            storyboard_setLostObjects(lostObjects)
            storyboard_setNewObjects(newObjects)
            areGoodConditions = true;
             end
             
          -- si je n'ai pas besoin d'objets
          else
              storyboard_sendTextGreen("\n"..STORYBOARD_MES_INTERFACE_012);
              storyboard_sendTextYellow(comment)
              local mes = STORYBOARD_MES_INTERFACE_012 .. comment;
                    storyboard_writeMessageAnswerForOtherPlayer(mes);
            
              storyboard_setLostObjects(lostObjects)
              storyboard_setNewObjects(newObjects)
              areGoodConditions = true;
          end
           end
         end
     end
      end
   end


   -- Si l'incantation n'est pas r�f�renc�e dans le sc�nario
   if (not wellLocated) or (not areGoodConditions) then
      -- on affiche un message par d�faut
      storyboard_sendTextYellow(storyboard_defaultMessage(message,STORYBOARD_SCENARIO_DEFAULT_INCANTATION));
   end

 return areGoodConditions;

end




-------------------------------------------------------------------------------
-- [ INTERACTION AVEC LES CIBLES ] --
-------------------------------------------------------------------------------

-- [ Declenchement d'un �venement fouiller ( pas de keywords )] --
function storyboard_useObjectOnTarget()
   storyboard_debugMessage("storyboard_useObjectOnTarget");

   local wellLocated = false;
   local areGoodConditions = false;
   
   for idLoc = 1, STORYBOARD_FS_NB_TARGET, 1 do
      
      local names         = STORYBOARD_SCENARIO_TARGET[idLoc].names
      local playerLife    = STORYBOARD_SCENARIO_TARGET[idLoc].playerLife
      local targetLife    = STORYBOARD_SCENARIO_TARGET[idLoc].targetLife
      local neededObjects = STORYBOARD_SCENARIO_TARGET[idLoc].neededObjects
      local newObjects    = STORYBOARD_SCENARIO_TARGET[idLoc].newObjects
      local lostObjects   = STORYBOARD_SCENARIO_TARGET[idLoc].lostObjects
      local keywords      = STORYBOARD_SCENARIO_TARGET[idLoc].keywords
      local comment       = STORYBOARD_SCENARIO_TARGET[idLoc].comment
      
      -- si j'ai la bonne cible sous les yeux
      if storyboard_isTheGoodTarget(names) then
     
     -- si j'ai pas besoin d'incanter
     if not ( storyboard_AreYouNeedKeywords(keywords) ) then

        wellLocated = true;

        -- Si la cible  dans le bon �tat corporel ( mort ou vivant )
        if storyboard_isTheSameLifeStateOnTarget(targetLife) then
        storyboard_debugMessage(targetLife);
        
           -- Si je suis dans le bon �tat corporel ( mort ou vivant )
           if storyboard_isTheSameLifeStateOnPlayer(playerLife) then
           
          -- si j'ai besoin d'objets
          if storyboard_AreYouNeedObjects(neededObjects) then
           
             -- Est ce que je les ai tous ?
             if storyboard_haveYouGotObjects(neededObjects) then
            storyboard_sendTextGreen("\n"..UnitName("target").." :");
            storyboard_sendTextYellow(comment);
            local mes = UnitName("target") .." : " .. comment;
                 storyboard_writeMessageAnswerForOtherPlayer(mes);
             
            storyboard_setLostObjects(lostObjects)
            storyboard_setNewObjects(newObjects)
            areGoodConditions = true;
             end
             
          -- si je n'ai pas besoin d'objets
          else
             storyboard_sendTextGreen("\n"..UnitName("target").." :");
             storyboard_sendTextYellow(comment);
             local mes = UnitName("target") .." : " .. comment;
                 storyboard_writeMessageAnswerForOtherPlayer(mes);
             
             storyboard_setLostObjects(lostObjects)
             storyboard_setNewObjects(newObjects)
             areGoodConditions = true;
          end
           end
        end
     end
      end
   end

   -- Si la fouille n'est pas r�f�renc�e dans le sc�nario
   if (not wellLocated) or (not areGoodConditions) then
      -- Si la cible est morte
      if storyboard_isTheSameLifeStateOnTarget(0) then
         -- on affiche un message par d�faut
         storyboard_sendAlertMessage(storyboard_defaultMessage(message,STORYBOARD_SCENARIO_DEFAULT_LOOT),1);
      else
         -- on demande une cible morte
         local mes = STORYBOARD_MES_CONSOLEINFO_010;
         storyboard_sendAlertMessage(mes,1);
      end
   end

      return areGoodConditions;

end




-- [ Declenchement d'un �venement incantation ( sur une cible )] --
function storyboard_useObjectAndKeywordsOnTarget(message)
   storyboard_debugMessage("storyboard_useObjectAndKeywordsOnTarget");
   
   local wellLocated = false;
   local areGoodConditions = false;
   
   for idLoc = 1, STORYBOARD_FS_NB_TARGET, 1 do
   
   local names          = STORYBOARD_SCENARIO_TARGET[idLoc].names
   local playerLife    = STORYBOARD_SCENARIO_TARGET[idLoc].playerLife
   local targetLife    = STORYBOARD_SCENARIO_TARGET[idLoc].targetLife
   local neededObjects = STORYBOARD_SCENARIO_TARGET[idLoc].neededObjects
   local newObjects    = STORYBOARD_SCENARIO_TARGET[idLoc].newObjects
   local lostObjects   = STORYBOARD_SCENARIO_TARGET[idLoc].lostObjects
   local keywords      = STORYBOARD_SCENARIO_TARGET[idLoc].keywords
   local comment       = STORYBOARD_SCENARIO_TARGET[idLoc].comment
   
   -- si j'ai la bonne cible sous les yeux
   if storyboard_isTheGoodTarget(names) then
      
      -- si j'ai besoin d'incanter
      if storyboard_AreYouNeedKeywords(keywords) then
     
     wellLocated = true;
     
     -- si j'ai les bonnes paroles
     if storyboard_areMatchBetweenYourAnswerAndYourWords(message,keywords) then
     
        -- Si la cible  dans le bon �tat corporel ( mort ou vivant )
        if storyboard_isTheSameLifeStateOnTarget(targetLife) then
           
           -- Si je suis dans le bon �tat corporel ( mort ou vivant )
           if storyboard_isTheSameLifeStateOnPlayer(playerLife) then
           
          -- si j'ai besoin d'objets
          if storyboard_AreYouNeedObjects(neededObjects) then
          
             -- Est ce que je les ai tous ?
             if storyboard_haveYouGotObjects(neededObjects) then
            storyboard_sendTextGreen("\n"..UnitName("target").." :");
            storyboard_sendTextYellow(comment);
            local mes = UnitName("target") .." : " .. comment;
            storyboard_writeMessageAnswerForOtherPlayer(mes);         
            storyboard_setLostObjects(lostObjects)
            storyboard_setNewObjects(newObjects)
            areGoodConditions = true;
             end
          
          -- si je n'ai pas besoin d'objets
          else
             storyboard_sendTextGreen("\n"..UnitName("target").." :");
             storyboard_sendTextYellow(comment);
             local mes = UnitName("target") .." : " .. comment;
                 storyboard_writeMessageAnswerForOtherPlayer(mes);
             storyboard_setLostObjects(lostObjects)
             storyboard_setNewObjects(newObjects)
             areGoodConditions = true;
          end
           end
        end
     end
      end
   end
   end
   
   -- Si la discussion n'est pas r�f�renc�e dans le sc�nario
   if (not wellLocated) or (not areGoodConditions) then
      -- Si la cible est vivante et humano�de
      if storyboard_isTheSameLifeStateOnTarget(1) and (UnitCreatureType("target") == "Humano\195\175de") then
         -- on affiche un message par d�faut
         storyboard_sendTextGreen("\n"..UnitName("target").." :");
         storyboard_sendTextYellow(storyboard_defaultMessage(message,STORYBOARD_SCENARIO_DEFAULT_TALK));
      else
         -- on demande une cible vivante et humano�de
         local mes = STORYBOARD_MES_CONSOLEINFO_011;
         storyboard_sendAlertMessage(mes,1);
      end
   end
   
   
   return areGoodConditions;

end
